package com.geekchique.runningbuddy.activity;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentReceiver;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.Spinner;

import com.geekchique.runningbuddy.R;
import com.geekchique.runningbuddy.controller.IMapOfJogController;
import com.geekchique.runningbuddy.controller.MapOfJogControllerImpl;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class MapOfJog extends MapActivity {
	
	private IMapOfJogController mapOfJogController;
	public LocationManager locationManager = null;
    protected LocationProvider provider;
	protected MyIntentReceiver myIntentReceiver = null; 
	private static final String MY_LOCATION_CHANGED_ACTION = new String("android.intent.action.LOCATION_CHANGED");
	private static final String PROVIDERNAME = "testjog";
	protected final IntentFilter myIntentFilter =  new IntentFilter(MY_LOCATION_CHANGED_ACTION);
	final Handler mHandler = new Handler();
	
  @Override
  public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      this.setContentView(R.layout.mapview);
      
      locationManager =  (LocationManager)getSystemService(Context.LOCATION_SERVICE);
      
      setupForGPSAutoRefreshing();
      
      mapOfJogController = new MapOfJogControllerImpl(this);
		
      mapOfJogController.createMapOfJogView(
    		  (Button)findViewById(R.id.go),
    		  (Spinner)findViewById(R.id.spinner1),
    		  android.R.layout.simple_spinner_dropdown_item,
    		  (MapView)findViewById(R.id.map));
      
  	}
  
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return mapOfJogController.onKeyDown(keyCode, event);
	}
	
	class MyIntentReceiver extends IntentReceiver {
		@Override
		public void onReceiveIntent(Context context, Intent intent) {
			Log.v("MAPOFJOG","!!!!!!updating the path!!!!!!!!!!!!!!!!!!!!");
			Location location = locationManager.getCurrentLocation(PROVIDERNAME);
			Log.i("MAPOFJOG", "longitude: "+location.getLongitude() + "latitude: "+location.getLatitude());
			mapOfJogController.setCurrentGPSLocationOfPhone(location);
			mapOfJogController.updateRunningPath();
		}
	}
	
	/**
	 * Restart the receiving, when we are back on line.
	 */
	@Override
	public void onResume() {
		super.onResume();
		
		/* As we only want to react on the LOCATION_CHANGED
		 * intents we made the OS send out, we have to 
		 * register it along with a filter, that will only
		 * "pass through" on LOCATION_CHANGED-Intents.
		 */
		this.registerReceiver(this.myIntentReceiver, this.myIntentFilter);
	}
	
	private void setupForGPSAutoRefreshing() {
		/* Register with out LocationManager to send us 
		 * an intent (whos Action-String we defined above)
		 * when  an intent to the location manager,
		 * that we want to get informed on changes to our own position.
		 * This is one of the hottest features in Android.
		 */
		final long MINIMUM_DISTANCECHANGE_FOR_UPDATE = 25; // in Meters
		final long MINIMUM_TIME_BETWEEN_UPDATE = 5000; // in Milliseconds
		List<LocationProvider> locProviders = locationManager.getProviders();
		provider = locProviders.get(0);
		provider = locationManager.getProvider(PROVIDERNAME);
		
		this.locationManager.requestUpdates(provider, MINIMUM_TIME_BETWEEN_UPDATE,	MINIMUM_DISTANCECHANGE_FOR_UPDATE, new Intent(MY_LOCATION_CHANGED_ACTION));
		
		/* Create an IntentReceiver, that will react on the
		 * Intents we made our LocationManager to send to us. 
		 */ 
		this.myIntentReceiver = new MyIntentReceiver();
	
		/* 
		 * In onResume() the following method will be called automatically!
		 * registerReceiver(this.myIntentReceiver, this.myIntentFilter); 
		 */
	}
	
	
	
	
}